#This file can be used to build binder using preinstalled LLVM/Clang
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0)
PROJECT(binder CXX C)
SET(VERSION 1.0.0)
#This actually will work even with C++11
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_VERBOSE_MAKEFILE ON)
#So far there are exceptions in config.cpp
set(LLVM_REQUIRES_EH ON)
set(USE_EXTERNAL_LLVM ON)
include("GNUInstallDirs")
set(LLVM_DIR_ORIG ${LLVM_DIR})
set(Clang_DIR_ORIG ${Clang_DIR})
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules ${CMAKE_MODULE_PATH})
SET(CMAKE_MODULE_PATH_ORIG "${CMAKE_MODULE_PATH}")
set (CMAKE_MODULE_PATH "${LLVM_DIR};${Clang_DIR};${CMAKE_MODULE_PATH}")
MESSAGE(STATUS  "binder: Using LLVM_DIR=${LLVM_DIR} to search for cmake configurations.")
MESSAGE(STATUS  "binder: Using Clang_DIR=${Clang_DIR} to search for cmake configurations.")
MESSAGE(STATUS  "binder: Using CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} to search for cmake configurations.")
add_custom_target(LLVMSupport)
find_package(Clang CONFIG QUIET PATHS /usr/lib64/cmake/clang /usr/lib/cmake/clang /usr/share/clang/cmake )
find_package(LLVM  CONFIG QUIET PATHS /usr/lib64/cmake/clang  /usr/lib/cmake/llvm  /usr/share/llvm/cmake )
if (Clang_FOUND AND LLVM_FOUND AND NOT LLVMCONFIG )
  set(CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR};${CMAKE_MODULE_PATH}") 
  MESSAGE(STATUS  "binder: cmake configurations  llvm and clang were found.  LLVM_CMAKE_DIR=${LLVM_CMAKE_DIR}")
  include(AddLLVM)
  include(LLVMConfig)
  include_directories( ${CLANG_INCLUDE_DIRS} )
  MESSAGE(STATUS  "binder: CLANG_INCLUDE_DIRS=${CLANG_INCLUDE_DIRS}")
else()
  MESSAGE(STATUS  "binder: llvm or clang cmake configurations were not found.")
  MESSAGE(STATUS  "binder: CLang_FOUND=${Clang_FOUND}")
  MESSAGE(STATUS  "binder: LLVM_FOUND=${LLVM_FOUND}")
  MESSAGE(STATUS  "binder: Trying to look for llvm-config.")
  set(LLVM_DIR ${LLVM_DIR_ORIG})
  set(Clang_DIR ${Clang_DIR_ORIG})
  SET(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH_ORIG}")
  MESSAGE(STATUS  "binder: Restored original LLVM_DIR=${LLVM_DIR}")
  MESSAGE(STATUS  "binder: Restored original Clang_DIR=${Clang_DIR}")
  if (LLVMCONFIG)
    MESSAGE(STATUS  "binder: The explicit location of  llvm-config is set to LLVMCONFIG=${LLVMCONFIG}")
    if (EXISTS ${LLVMCONFIG})
      set (LLVMCONFIGEXE ${LLVMCONFIG})
    else()
      MESSAGE(FATAL_ERROR  "binder: File ${LLVMCONFIG} does not exist")
    endif()
  else()
    MESSAGE(STATUS  "binder: Searching for llvm-config in ${LLVM_DIR}/bin $ENV{PATH} /usr/bin/ /usr/local/bin")
    find_program(LLVMCONFIGEXE NAMES llvm-config HINTS  ${LLVM_DIR}/bin $ENV{PATH} /usr/bin/ /usr/local/bin)
  endif()
  if (LLVMCONFIGEXE)
    MESSAGE(STATUS  "binder: llvm-config found in ${LLVMCONFIGEXE}")
    execute_process (COMMAND ${LLVMCONFIGEXE}  --version     OUTPUT_VARIABLE  LLVMVERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
    execute_process (COMMAND ${LLVMCONFIGEXE}  --libdir      OUTPUT_VARIABLE  LLVMLIBDIR OUTPUT_STRIP_TRAILING_WHITESPACE)
    execute_process (COMMAND ${LLVMCONFIGEXE}  --includedir  OUTPUT_VARIABLE  LLVMINCLUDEDIR OUTPUT_STRIP_TRAILING_WHITESPACE)
    execute_process (COMMAND ${LLVMCONFIGEXE}  --cxxflags    OUTPUT_VARIABLE  LLVMCXXFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
    set(LLVM_COMPILE_FLAGS ${LLVMCXXFLAGS})
    include_directories( ${LLVMINCLUDEDIR})
    include_directories( ${CLANG_INCLUDE_DIRS} )
    link_directories(${LLVMLIBDIR} )
    string(REPLACE "." ";" VERSION_LIST ${LLVMVERSION})
    list(GET VERSION_LIST 0 LLVM_VERSION_MAJOR)
    list(GET VERSION_LIST 1 LLVM_VERSION_MINOR)
    list(GET VERSION_LIST 2 LLVM_VERSION_PATCH)
    MESSAGE(STATUS  "binder: llvm version from ${LLVMCONFIGEXE} is ${LLVMVERSION}")
    MESSAGE(STATUS  "binder: llvm library dir from ${LLVMCONFIGEXE} is ${LLVMLIBDIR}")
    MESSAGE(STATUS  "binder: llvm includedir dir from ${LLVMCONFIGEXE} is ${LLVMINCLUDEDIR}")
  else()
    MESSAGE(FATAL_ERROR  "binder: llvm-config not found")
  endif()
endif()
MESSAGE(STATUS  "binder: LLVM_VERSION_MAJOR=${LLVM_VERSION_MAJOR}")
MESSAGE(STATUS  "binder: LLVM_VERSION_MINOR=${LLVM_VERSION_MINOR}")
MESSAGE(STATUS  "binder: LLVM_VERSION_PATCH=${LLVM_VERSION_PATCH}")
macro(add_clang_executable name)
  add_executable( ${name} ${ARGN} )
#  set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
    message(STATUS "binder: We have cmake functions to update LLVM_COMPILE_FLAGS")
    if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
      list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
    elseif (MSVC)
      list(APPEND LLVM_COMPILE_FLAGS "/GR-")
    endif ()
endmacro(add_clang_executable)
include_directories(source)
add_subdirectory(source)
